﻿using FluentValidation;
using Mes.Application.Common;
using Mes.Application.DTO.Inputs.WorkOrders;
using Mes.Application.DTO.Results.WorkOrders;
using Mes.Application.Services.WorkOrders;
using Mes.Application.Web.Validators.WorkOrders;
using Mes.Domain.BoundedContext;
using Microsoft.AspNetCore.Mvc;

namespace Mes.Application.Web.Controllers.WorkOrders
{
    /// <summary>
    /// 作业控制器
    /// </summary>
    [ApiController]
    [Route("/api/v1")]
    public class JobController : ControllerBase
    {
        private readonly IJobService _jobService;
        private readonly IValidator<AddingJobInput> _addingJobInputValidator;
        private readonly IValidator<UpdatingJobInput> _updatingJobInputValidator;
        private readonly IValidator<DeletingJobInput> _deletingJobInputValidator;
        private readonly IValidator<GettingJobInput> _gettingJobInputValidator;

        /// <summary>
        /// 作业控制器
        /// </summary>
        /// <param name="jobService"></param>
        /// <param name="addingJobInputValidator"></param>
        /// <param name="updatingJobInputValidator"></param>
        /// <param name="deletingJobInputValidator"></param>
        /// <param name="gettingJobInputValidator"></param>
        public JobController(IJobService jobService, IValidator<AddingJobInput> addingJobInputValidator, IValidator<UpdatingJobInput> updatingJobInputValidator, IValidator<DeletingJobInput> deletingJobInputValidator, IValidator<GettingJobInput> gettingJobInputValidator)
        {
            _jobService = jobService;
            _addingJobInputValidator = addingJobInputValidator;
            _updatingJobInputValidator = updatingJobInputValidator;
            _deletingJobInputValidator = deletingJobInputValidator;
            _gettingJobInputValidator = gettingJobInputValidator;
        }

        /// <summary>
        /// 新增作业
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("job")]
        public async Task<ActionResult> AddJobAsync([FromBody] AddingJobInput input)
        {
            try
            {
                var result = await _addingJobInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _jobService.AddJobAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑作业
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPatch("job")]
        public async Task<ActionResult> UpdateJobAsync([FromBody] UpdatingJobInput input)
        {
            try
            {
                var result = await _updatingJobInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _jobService.UpdateJobAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除作业
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete("job")]
        public async Task<ActionResult> DeleteJobAsync([FromQuery]DeletingJobInput input)
        {
            try
            {
                var result = await _deletingJobInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                await _jobService.DeleteJobAsync(input);
                return Ok();
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询作业实体
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("job")]
        public async Task<ActionResult<JobDetailsResult>> GetJobAsync([FromQuery]GettingJobInput input)
        {
            try
            {
                var result = await _gettingJobInputValidator.ValidateAsync(input);
                if (!result.IsValid)
                {
                    return BadRequest(result.Errors.First().ErrorMessage);
                }
                var response = await _jobService.GetJobAsync(input);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 查询作业队列
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("jobqueue")]
        public async Task<ActionResult<PagedItemsData<GettingJobQueueResult>>> GetJobQueueAsync([FromQuery] GettingJobQueueInput input)
        {
            try
            {
                var response = await _jobService.GetJobQueueAsync(input);
                return Ok(response);
            }
            catch (CustomException ex)
            {
                return BadRequest(ex.Message);
            }
        }
    }
}
